<%@page import="shmged.com.shlcm.workflow.setparticipant.TpNeedRole"%>
<%@page import="java.util.Map"%>
<%@page import="shmged.com.shlcm.workflow.setparticipant.TpContainerHelper"%>
<%@page import="wt.org.WTUser"%>
<%@page import="java.util.List"%>
<%@page import="wt.project.Role"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.ArrayList"%>
<%@page import="wt.util.WTMessage"%>
<%@page import="wt.util.WTContext"%>
<%@page import="java.util.Locale"%>
<%@page import="shmged.com.shlcm.workflow.setparticipant.TpWorkflowHelper"%>
<%@page import="shmged.com.shlcm.workflow.WorkflowConfigBean"%>
<%@page import="shmged.com.shlcm.workflow.TpSettings"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<style type="text/css">
.SpaceStyle {
	text-decoration: none;
}
</style>
<%
String oid = request.getParameter("oid");
String isNeedSearchButton = request.getParameter("isNeedSearchButton");
boolean isNeedConfigPath = true;
ArrayList<TpNeedRole> tpNeededRoles = null;
if(TpWorkflowHelper.isSetupParticipants(oid, "setupParticipants")){
	String valRute = (String)TpWorkflowHelper.getVariableValue(oid, "TpsetparticipantRute");
	if(null != valRute){
	   tpNeededRoles = TpWorkflowHelper.getWorkflowTpNeedRole(valRute);
	}
}
if( null  != tpNeededRoles){
	String contextPath = request.getContextPath();
	ArrayList<Map<String,String>> topGroupUsers = TpContainerHelper.getTopGroupUsers(TpSettings.getSection("participantConfig").get("topGroupName", "SHMGED"));	
	if(null != tpNeededRoles){
		Locale locale = WTContext.getContext().getLocale();
		String workflowResource = "shmged.com.shlcm.workflow.resources.WorkflowRB";
		String GET_CONFIGPATH_ERROR_MESSAGE = WTMessage.getLocalizedMessage(workflowResource,"GET_CONFIGPATH_ERROR",null,locale);
		String PRINCIPAL_TITLE = WTMessage.getLocalizedMessage(workflowResource,"PRINCIPAL_TITLE",null,locale);
		String ROLE_LIST = WTMessage.getLocalizedMessage(workflowResource,"ROLE_LIST",null,locale);
		String SELECT_USER = WTMessage.getLocalizedMessage(workflowResource,"SELECT_USER",null,locale);
		String ADD_BUTTON = WTMessage.getLocalizedMessage(workflowResource,"ADD",null,locale);
		String NOT_SUPORT_AJAX_MESSAGE = WTMessage.getLocalizedMessage(workflowResource,"NOT_SUPORT_AJAX",null,locale);
		String AJAX_ERROR_MESSAGE = WTMessage.getLocalizedMessage(workflowResource,"AJAX_ERROR",null,locale);
		String SELECT_USER_MESSAGE = WTMessage.getLocalizedMessage(workflowResource,"SELECT_USER",null,locale);
		String USER_EXIST_MESSAGE = WTMessage.getLocalizedMessage(workflowResource,"USER_EXIST",null,locale);
		String NEED_USER_MESSAGE = WTMessage.getLocalizedMessage(workflowResource,"NEED_USER",null,locale);
		String INPUT_USER_MESSAGE = WTMessage.getLocalizedMessage(workflowResource,"INPUT_USER",null,locale);
		String SEARCH_MESSAGE = WTMessage.getLocalizedMessage(workflowResource,"SEARCH",null,locale);
		String USER_NAME_MESSAGE = WTMessage.getLocalizedMessage(workflowResource,"USER_NAME",null,locale);
		
		String SINGATURE_MESSAGE = WTMessage.getLocalizedMessage(workflowResource,"SINGATURE",null,locale);
		String NEED_MESSAGE = WTMessage.getLocalizedMessage(workflowResource,"NEED",null,locale);
		String SELECTABLE_MESSAGE = WTMessage.getLocalizedMessage(workflowResource,"SELECTABLE",null,locale);
		String SETPARTICIPANT_WRONGROLEMESSAGE = WTMessage.getLocalizedMessage(workflowResource,"SETPARTICIPANT_WRONGROLEMESSAGE",null,locale);	
		String JUNDAIBIAO = WTMessage.getLocalizedMessage(workflowResource,"JUNDAIBIAO",null,locale);	
		
		String YONGHUFANGDAIBIAO = WTMessage.getLocalizedMessage(workflowResource,"YONGHUFANGDAIBIAO",null,locale);	
		String FZSYSZGLD = WTMessage.getLocalizedMessage(workflowResource,"FZSYSZGLD",null,locale);	
		
		String XITONGZHUGUANLINGDAO = WTMessage.getLocalizedMessage(workflowResource,"XITONGZHUGUANLINGDAO",null,locale);	
		String ZONGGONGCHENGSHI = WTMessage.getLocalizedMessage(workflowResource,"ZONGGONGCHENGSHI",null,locale);
		
	    Map<Boolean,Map<Role, List<WTUser>>> tpDataBack = TpWorkflowHelper.getUserByRole(oid,tpNeededRoles);
	    Boolean initFromRole =  tpDataBack.keySet().iterator().next();
	    Map<Role, List<WTUser>> workflowRoleUser = tpDataBack.get(initFromRole);
%>

<script>
	var oid = '<%=oid%>';
	var locale = '<%=locale%>';
	var isNeedConfigPath = '<%=isNeedConfigPath%>';
	var containerRoleList = new Array();
	var addPrincipalMap = new Array();
	var buttonFlag = true;

	//验证必选人员是不是已经选择了人员
	function validata(){
		//对于取消不验证用户必须性
		var userEvent = document.getElementsByName("userEvent");
		if(userEvent){
			for(var i = 0 ; i < userEvent.length;i++){
				if(userEvent[i].checked){
					if('取消' == userEvent[i].value){
						return true;
					}
				}
			}
		}
		var flag = true;
		if(isNeedConfigPath == 'true'){
			for(var i = 0 ; i < addPrincipalMap.length ; i++){
				if (addPrincipalMap[i].mustAdd == "*" ){
					if(addPrincipalMap[i].userArray == 0){
						flag = false;
						alert(addPrincipalMap[i].roleDisName+" "+'<%=NEED_USER_MESSAGE%>');
						break;
					}
				}
			}
		}
		return flag;
	}

//查找用户	
function searchUser(){
	var userName = document.getElementById("userName").value;
    if(userName == ""){
				alert("<%=INPUT_USER_MESSAGE%>");
	}else{
		var selectElement = document.getElementById("containerRoleList");
		selectElement.selectedIndex = -1;
		userName=encodeURI(userName );
				
		var xmlHttpRequest;
	    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        	xmlHttpRequest = new XMLHttpRequest();
        	if (xmlHttpRequest.overrideMimeType) {
        		xmlHttpRequest.overrideMimeType('text/xml');
        	}
      	} else if (window.ActiveXObject) { // IE
      		try {
        		xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
        	} catch (e) {
        		try {
          		xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
          	    } catch (e) {
          		   alert('<%=NOT_SUPORT_AJAX_MESSAGE%>');
          	    }
       		}
      	}
       	
		xmlHttpRequest.onreadystatechange=function(){
			if(xmlHttpRequest.readyState==4){
				 if (xmlHttpRequest.status == 200) {
				  		var userNameList = xmlHttpRequest.responseText;
				  		var userNameArray = userNameList.split("`");
							updateSearchUser(userNameArray);
				 }else{
				  		alert("<%=AJAX_ERROR_MESSAGE%>");
				  	}
		    }
		};
		xmlHttpRequest.open("GET","<%=contextPath%>/shmged/jsp/com/shlcm/workflow/setparticipant/searchUser.jsp?userName="+userName+"&locale="+locale+"&oid="+oid,true);
		xmlHttpRequest.setRequestHeader("If-Modified-Since","0");
		xmlHttpRequest.send(null);
	}
}
		
function updateSearchUser(userArray){
	var userList = document.getElementById("userList");
	userList.length=0;
	for(var i = 1 ; i <userArray.length-1;i++){
		 var str = userArray[i];
		 userList.options.add(new Option(str,str));
	}
	userList.selectedIndex = 0;
}
		
function showUser(){
	//alert("showUser ...");
	var containerSelect = document.getElementById("containerRoleList");
	var userListSelect = document.getElementById("userList");
	userListSelect.length = 0 ;
	var tempUsers = containerSelect.options[containerSelect.selectedIndex].value;
	if(null == tempUsers || "" == tempUsers){
		return;
	}
	//var tempUserList = containerRoleList[containerSelect.selectedIndex].userArray;
	var tempUserList = tempUsers.split(";");
	for(var i = 0 ; i < tempUserList.length ; i++){
		var userInfos = tempUserList[i].split(",");
		if(userInfos.length == 2){
		    userListSelect.options.add(new Option(userInfos[1],userInfos[0]));
		}
	}
}
	
function isExist(index,value){
		var flag = false;
		for(var i = 0 ; i < addPrincipalMap[index].userArray.length ; i++){
			if(addPrincipalMap[index].userArray[i] == value){
				flag = true;
				break;
			}
		}
		return flag;
}
	
function addUser(index){
		if(buttonFlag != true){
			return ;
		}
		buttonFlag = false;
		var userListSelect = document.getElementById("userList");
		if(userListSelect.selectedIndex == -1){
			alert("<%=SELECT_USER_MESSAGE%>");
			buttonFlag = true;
			return;
		}
		//alert(containerRoleSelect[containerRoleSelect.selectedIndex].text);
		//alert(userListSelect[userListSelect.selectedIndex].text);
		//alert(addPrincipalMap[index].needRole);
		if(!isExist(index,userListSelect[userListSelect.selectedIndex].text)){
			addPrincipalMap[index].userArray[addPrincipalMap[index].userArray.length] = userListSelect[userListSelect.selectedIndex].text;
			//saveUser();
			var dE = document.getElementById("role"+index);
			var tempCount = addPrincipalMap[index].userArray.length-1;
			var temp = index+","+tempCount;
			var tempID = "role"+index+"a"+tempCount;
			//alert(userListSelect[userListSelect.selectedIndex].text);
			//alert(tempID);
			dE.innerHTML=dE.innerHTML+"<a href='javascript:removeUser("+temp+")' id='"+tempID+"'>"+userListSelect[userListSelect.selectedIndex].text+";<span>&nbsp</span></a>";
		}else{
			alert("<%=USER_EXIST_MESSAGE%>");
			buttonFlag = true;
		}
		buttonFlag = true;
}
	
	function removeUser(roleIndex,userIndex){
		if(buttonFlag != true){
			return ;
		}
		buttonFlag = false;
		addPrincipalMap[roleIndex].userArray.splice(userIndex,1);
		//saveUser();
		var divE = document.getElementById("role"+roleIndex);
		var aE = document.getElementById("role"+roleIndex+"a"+userIndex);
		divE.removeChild(aE);
		var childrens = divE.children;
		var count = userIndex;
		for(var i = userIndex ; i < childrens.length ; i++){
			if(childrens[i].href != undefined && childrens[i].href != ""){
				childrens[i].href = "javascript:removeUser("+roleIndex+","+count+")";
				childrens[i].id = "role"+roleIndex+"a"+count;
				count++;
			}
		}
		buttonFlag = true;
	}
	
	function changePath(){

		var path_check = document.getElementsByName("path_check");
		//var buttonRole = document.getElementById("buttonRole"+index);
		for(var i = 0 ; i < path_check.length ; i++){
			for(var j = 0 ; j < addPrincipalMap.length ; j++){
				if(addPrincipalMap[j].path == path_check[i].id){
					if(path_check[i].checked == true){
						addPrincipalMap[j].changePath = "1";
					}else{
						addPrincipalMap[j].changePath = "";
					}
				}
			}
		}
	}
	
	function initalPath(){
		var path_check = document.getElementsByName("path_check");
		for(var i = 0 ; i < path_check.length ; i++){
			changePath(i);
		}
		path_check[0].disabled = true;
		path_check[path_check.length-2].disabled = true;
		path_check[path_check.length-1].disabled = true;
	}
</script>
<B><%=PRINCIPAL_TITLE%></B>

<table width="100%" border="0">
	<tr>
		<td align="right"></td>
		<td></td>
		<td></td>
	</tr>
	<%
		if("true".equals(isNeedSearchButton)){
	%>
	<tr>
		<td width="200"><B><%=USER_NAME_MESSAGE%>:</B><input type="text"
			name="userName" value="" size="10"></td>
		<td width="200"><input type="button" value="<%=SEARCH_MESSAGE%>"
			onclick="searchUser()"></td>
	</tr>
	<%
		}
	%>
	<!--<tr>
		<td width="15%"><B><label><%=ROLE_LIST%></label></B></td>
		<td width="15%"><B><label><%=SELECT_USER%></label></B></td>
		<td></td>
	</tr>-->

	<tr>
		<td valign="top" height="100%">
		<select name="containerRoleList"
			id="containerRoleList" multiple="multiple"
			style="width: 170; height: 300" onChange="showUser()" onselect="showUser()">
		  <%
		  //<%= ((0 == iu )? "selected='selected'" :"")  onChange="showUser()"
		   Map<String,String> groupUser = null;
		   for(int iu = 0 ;iu < topGroupUsers.size();iu ++){
			   groupUser = topGroupUsers.get(iu);
		       String TPgroupKey = groupUser.keySet().iterator().next();
		       String TPgroupValue = groupUser.get(TPgroupKey);
		  %>
			 <option value="<%=TPgroupValue%>" <%= ((0 == iu )? "selected='selected'" :"")%>><%=TPgroupKey%></option>
		  <%}%>
		</select>
		</td>
		<td valign="top">
			<div id="userListDiv">
				<select name="userList" id="userList" multiple="multiple"
					style="width: 160; height: 300;">
				</select>
			</div>
		</td>
		<td width="100%">
			<table border="0">
				<%
					for(int i = 0 ;i < tpNeededRoles.size(); i++){
						TpNeedRole tmpRole = tpNeededRoles.get(i);
						String tmpDis = tmpRole.getRole().getLocalizedMessage(locale);
						String roleName = tmpRole.getRoleName();
				%>
				<!--<option value='<%=tmpRole.toString()%>'><%=tmpDis%></option>-->
				<script type="text/javascript">
					var tempRoleAndUserObj = new Object();
					tempRoleAndUserObj.roleName='<%=roleName%>';
					tempRoleAndUserObj.roleDisName='<%=tmpDis%>';
					tempRoleAndUserObj.mustAdd='<%=tmpRole.isMust() ? "*":""%>'
					var tempRoleAndUserObjArray = new Array();
				</script>
				<tr>
					<td nowrap="nowrap">
						<div id="<%=tmpDis%>">
							<input type="button" id="buttonRole<%=i%>"
								value="<%=ADD_BUTTON%>" onclick="addUser('<%=i%>')">
							&nbsp;<%=tmpRole.isMust() ? "*":""%><%=tmpDis%>:
						</div>
					</td>
					<td>
						<div id="role<%=i%>">
							<%
							    List<WTUser> roleUserList = null;
							    if(initFromRole){
							    	roleUserList = workflowRoleUser.get(tmpRole.getUserRole());
							    }else{
							    	roleUserList = workflowRoleUser.get(tmpRole.getRole());
							    }
								for(int j = 0 ; roleUserList != null && j < roleUserList.size(); j++){
									WTUser user = (WTUser)roleUserList.get(j);
							%>
							<a href="javascript:removeUser(<%=i%>,<%=j%>);" id="role<%=i%>a<%=j%>" ><%=user.getFullName()%>;</a>
							<script>
								tempRoleAndUserObjArray[tempRoleAndUserObjArray.length] = '<%=user.getName()%>';
							</script>
							<%}%>
						</div>
					</td>
				</tr>
				<script type="text/javascript">
					tempRoleAndUserObj.userArray = tempRoleAndUserObjArray;
					addPrincipalMap[addPrincipalMap.length] = tempRoleAndUserObj;
				</script>
				<%
					}
				%>
			</table>
		</td>
	</tr>
</table>
<script>
	//initalPath();
</script>
<%
	}
}
%>